package com.spark.service;

import com.alibaba.fastjson.JSON;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;

import static org.apache.hadoop.yarn.webapp.MimeType.JSON;
@ServerEndpoint("/websocket")
public class WebSocket {
    WebShowService webShowService =new WebShowService();
    @OnMessage
    public void onMessage(String message, Session session) throws IOException, InterruptedException {
        while (true){
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("titleName",webShowService.queryWebLogs().get("titleName"));
            map.put("titleCount",webShowService.queryWebLogs().get("titleCount"));
            map.put("titleSum", webShowService.titleCount());
            session.getBasicRemote().sendText(com.alibaba.fastjson.JSON.toJSONString(map));
            Thread.sleep(1000);
            map.clear();
        }
    }
    @OnOpen
    public void onOpen(){
        System.out.println("Client connected");
    }
    @OnClose
    public void onClose(){
        System.out.println("Connected closed");
    }

}
